//author:jxx
import store from "@/store/index";
//此处是对表单的方法，组件，权限操作按钮等进行任意扩展(方法扩展可参照SellOrder.js)
let extension = {
  components: {
    //动态扩充组件或组件路径
    //表单header、content、footer对应位置扩充的组件
    gridHeader: "", //{ template: "<div>扩展组xx件</div>" },
    gridBody: "",
    gridFooter: "",
    //弹出框(修改、编辑、查看)header、content、footer对应位置扩充的组件
    modelHeader: "",
    modelBody: "",
    modelFooter: ""
  },  
  buttons: { view: [], box: [], detail: [] }, //扩展的按钮
  methods: {
     //事件扩展
    getOption(field) {
      let option;
      this.editFormOptions.forEach(x => {
        x.forEach(item => {
          if (item.field == field) {
            option = item;
          }
        });
      });
      return option;
    },
    onInit() {
      //新建窗口的宽和高
      this.boxOptions.width = 1000;
      this.boxOptions.height = 600;
      let _userInfo = store.getters.getUserInfo();
      let spare10 = this.getOption("spare10");
      let smsId = this.getOption("smsId");
      smsId.onChange = (val, option) => {
        console.log(option);
        spare10.hidden = false;
        let data = option.data.filter(item => item.key == val);
        if (data.length > 0) {
          this.editFormFields[spare10.field] = data[0].remark;
        } else {
          spare10.hidden = true;
        }
      };
    },

    direct() {
      let isMessage = this.getOption("isMessage");
      let smsId = this.getOption("smsId");
      let smsMode = this.getOption("smsMode");
      let signCode = this.getOption("signCode"); //短信签名
      let contactPerson = this.getOption("contactPerson");
      let contactPhone = this.getOption("contactPhone");

      isMessage.hidden = false;
      if (this.editFormFields[isMessage.field] == 1) {
        smsId.required = true;
        smsMode.required = true;
        signCode.required = true;
      }
      smsId.hidden = false;
      smsMode.hidden = false;
      signCode.hidden = false;
      contactPerson.hidden = false;
      contactPhone.hidden = false;
    },
    async modelOpenBeforeAsync(row) {
      let smsId = this.getOption("smsId");
      let smsMode = this.getOption("smsMode");
      let signCode = this.getOption("signCode");
      let spare10 = this.getOption("spare10");
      let isMessage = this.getOption("isMessage");
      this.editFormFields[signCode.field] = "";
      this.editFormFields[smsId.field] = "";
      this.editFormFields[smsMode.field] = "";
      smsId.hidden = false;
      smsMode.hidden = false;
      signCode.hidden = false;
      spare10.hidden = true;
      smsId.required = false;
      smsMode.required = false;
      signCode.required = false;

      isMessage.onChange = (val, option) => {
        if (val == 1) {
          smsId.hidden = false;
          smsMode.hidden = false;
          signCode.hidden = false;
          smsId.required = true;
          smsMode.required = true;
          signCode.required = true;
        } else {
          this.editFormFields[signCode.field] = "";
          this.editFormFields[smsId.field] = "";
          this.editFormFields[smsMode.field] = "";
          smsId.hidden = true;
          smsMode.hidden = true;
          signCode.hidden = true;
          spare10.hidden = true;
          smsId.required = false;
          smsMode.required = false;
          signCode.required = false;
        }
      };
      return true;
    },
    rowClick({ row, column, event }) {
      //查询界面table点击行选中当前行
      console.log(row);
      this.$refs.table.$refs.table.toggleRowSelection(row);
      return true;
    },
    delBefore(ids, rows) {
      console.log(rows, "rows");
      let code = rows.map(i => i.supplierCode);
      let params = {
        page: 1,
        rows: 30,
        sort: "createDate",
        order: "desc",
        wheres: `[{\"name\":\"supplierCode\",\"value\":\"${code.join(
          ","
        )}\",\"displayType\":\"checkbox\"}]`
      };
      this.http.post("api/Products/getPageData", params).then(res => {
        if (res.rows.length != 0) {
          this.$Message.error("当前供应商下有产品，暂时不能删除");
        } else {
          this.$Modal.confirm({
            title: "删除警告!",
            content:
              '<p style="color: red;font-weight: bold;letter-spacing: 3px;">确认要删除选择的数据吗?</p>',
            onOk: () => {
              this.http.post("api/SupplierNews/del", ids).then(res => {
                this.$Message.success("删除成功");
                this.refresh();
              });
            }
          });
        }
      });
    },
    modelOpenAfter(row) {
      // 弹出框是新建还是编辑 isEDIT  true 为编辑 false 为新建
      let isEDIT = this.currentAction == this.const.EDIT;
      let supplierType = this.getOption("supplierType");
      let systemCode = this.getOption("systemCode");
      systemCode.data = [];
      let systemName = this.getOption("systemName");
      let appKey = this.getOption("appKey");
      let appSecret = this.getOption("appSecret");
      let cmmcode = this.getOption("cmmcode");
      // let supplierUrl = this.getOption("supplierUrl");
      let hotelId = this.getOption("hotelId");
      let ver = this.getOption("ver");
      let appkey_xms = this.getOption("appkey_xms");
      let session = this.getOption("session");
      if (isEDIT) {
        appKey.hidden = false;
        appSecret.hidden = false;
        appKey.disabled = true;
        appSecret.disabled = true;
        console.log(row,"row")
        this.editFormFields.systemCode=(row.systemCode==null||row.systemCode=="")?"":row.systemCode.split(',')
        // row.systemCode.split(',')
        if(row.supplierType=="0"){
          // console.log("1213434")
          systemCode.hidden=false
          systemCode.required = false;
        }
      } else {
        appKey.disabled = false;
        appSecret.disabled = false;
      }
      cmmcode.hidden = true;
      // supplierUrl.hidden = true;
      hotelId.hidden = true;
      ver.hidden = true;
      appkey_xms.hidden = true;
      session.hidden = true;
      if (row !== undefined) {
        console.log(row.supplierParams);
        this.http
          .get(
            "api/SupplierNews/GetSupplierSystem?type=" + row.supplierType,
            true
          )
          .then(reslut => {
            console.log(reslut, "");
            // systemCode.data.push(...reslut.data);
            if (reslut.data != null) {
              // systemCode.data.push(...reslut.data);

              systemCode.data = reslut.data.map(i => {
                return {
                  key: i.id,
                  value: i.key,
                  label: i.label,
                  children: i.children
                };
              });
            } else {
              systemCode.data = [];
            }
          });
        if (row.supplierParams != null) {
          let text = row.supplierParams;

          let array = text.split(",");
          array.forEach(j => {
            this.editFormOptions.forEach(x => {
              x.forEach(item => {
                if (item.field == j) {
                  item.hidden = false;
                }
              });
            });
          });
        }
      }
      supplierType.onChange = (val, option) => {
        appKey.hidden = true;
        appSecret.hidden = true;
        cmmcode.hidden = true;
        // supplierUrl.hidden = true;
        hotelId.hidden = true;
        ver.hidden = true;
        appkey_xms.hidden = true;
        session.hidden = true;
        if (val == 0) {
          systemCode.hidden = true;
          systemCode.required = false;
        } else if (val == 4) {
          systemCode.hidden = false;
          systemCode.required = false;
        } else {
          systemCode.hidden = false;
          systemCode.required = true;
        }
        //this.editFormFields[systemCode.field]=0;
        //通过http从后台加载数据源
        console.log(systemCode.data, "后台数据");
        systemCode.data = [];
        console.log("tag", this.editFormFields.systemCode);
        //  this.editFormFields[systemCode.field] = ""
        this.editFormFields.systemCode = [];
        this.http
          .get("api/SupplierNews/GetSupplierSystem?type=" + val, true)
          .then(reslut => {
            console.log(reslut);
            if (reslut.data != null) {
              // systemCode.data.push(...reslut.data);

              systemCode.data = reslut.data.map(i => {
                return {
                  key: i.id,
                  value: i.key,
                  label: i.label,
                  children: i.children
                };
              });
            } else {
              systemCode.data = [];
            }
          });
      };
      systemCode.onChange = (val, option) => {
        //系统名称选择数据
        let arr = [];
        arr = option[2].remark.split(",");
        sessionStorage.setItem("remarkarr", arr);
        arr.forEach(j => {
          this.editFormOptions.forEach(x => {
            x.forEach(item => {
              if (item.field == j) {
                item.hidden = false;
                console.log(item.field);
              } else {
              }
            });
          });
        });
      };
    },
    addBefore(formData) {
      //新建保存前formData为对象，包括明细表
      return this.veriFromTable(formData);
    },
    updateBefore(formData) {
      //编辑保存前formData为对象，包括明细表、删除行的Id
      return this.veriFromTable(formData);
    },
    // 保存前编辑
    veriFromTable(formData) {
      let systemCode = this.getOption("systemCode");
     let a  = systemCode.data.filter(i => {
        return i.value == formData.mainData.systemCode;
      });
      console.log(a,"formData.mainData.systemName")
      formData.mainData.systemName=a.length>0? a[0].label:""
       formData.mainData.systemCode=this.editFormFields.systemCode.toString()  
      formData.mainData.supplierParams = sessionStorage.getItem("remarkarr");
      return true;
    }
  }
};
export default extension;
